/**
 * 
 */
package edu.rit.cs.web.panels;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponentLabel;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;

import edu.rit.cs.web.pojos.Business;
import edu.rit.cs.ws.uddi.UDDIServiceImpl;

/**
 * @author Abhishek Shrestha (axs1430)
 * @author Juan Saldana (jjs6109)
 * @author Victor Alvarez (vxa1557)
 */
public class StockMarketPanel extends Panel {
  private static final long serialVersionUID = 1L;

  final UDDIServiceImpl uddiService = new UDDIServiceImpl();
  private Business business;

  public StockMarketPanel(final String id) {
    super(id);
    setOutputMarkupId(true);

    final Form<Business> serviceForm = new MarketForm("marketForm");
    serviceForm.add(new AjaxButton("submitBtn") {
      private static final long serialVersionUID = 1L;

      @Override
      protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
        target.add(StockMarketPanel.this);
        try {
          uddiService.publish(serviceForm.getModelObject());
          info ("Service published");
          serviceForm.setModelObject(new Business());

        } catch (Exception e) {
          error (e.getMessage());
          e.printStackTrace();
        }
      }
      
      @Override
      protected void onError(AjaxRequestTarget target, Form<?> form) {
        target.add(StockMarketPanel.this);
      }
    });

    add(serviceForm);
    add(new FeedbackPanel("feedback"));
    
    final DropDownChoice<Business> ddc = new DropDownChoice<Business>("businesses", new BusinessesListDetachableModel());
    ddc.setModel(new PropertyModel<Business>(this, "business"));

    ddc.setChoiceRenderer(new IChoiceRenderer<Business>() {
      private static final long serialVersionUID = 1L;

      @Override
      public Object getDisplayValue(Business object) {
        return object.getName();
      }

      @Override
      public String getIdValue(Business object, int index) {
        return object.getKey();
      }
    });
    
    ddc.add(new AjaxFormComponentUpdatingBehavior("onchange") {
      private static final long serialVersionUID = 1L;

      @Override
      protected void onUpdate(AjaxRequestTarget target) {
        target.add(StockMarketPanel.this);
      }
    });
    add(ddc);
    add(new AjaxLink<Void>("deleteBtn") {
      private static final long serialVersionUID = 1L;

      @Override
      public void onClick(AjaxRequestTarget target) {
        target.add(StockMarketPanel.this);
        try {
          uddiService.deleteBusiness(business.getKey());
          business = new Business();
          info("Service deleted");
        } catch (Exception e) {
          e.printStackTrace();
          error (e.getMessage());
        }
      }
    });
  }

  /**
   * @author Abhishek Shrestha (axs1430)
   * @author Juan Saldana (jjs6109)
   * @author Victor Alvarez (vxa1557)
   */
  public class BusinessesListDetachableModel extends LoadableDetachableModel<List<Business>> {
    private static final long serialVersionUID = 1L;

    @Override
    protected List<Business> load() {
      try {
        return uddiService.findAllBusinesses();
      } catch (Exception e) {
        e.printStackTrace();
      }

      return new ArrayList<Business>(0);
    }
  }

  /**
   * @author Abhishek Shrestha (axs1430)
   * @author Juan Saldana (jjs6109)
   * @author Victor Alvarez (vxa1557)
   */
  protected class MarketForm extends Form<Business> {
    private static final long serialVersionUID = 1L;

    public MarketForm(String id) {
      super(id, new CompoundPropertyModel<Business>(new Business()));

      String[] fieldIds = new String[] {
         "name"
        ,"description"
        ,"contact.name"
        ,"contact.phone"
        ,"contact.email"
        ,"service.name"
        ,"service.description"
        ,"service.url"
      };
      addField(fieldIds);
    }

    private void addField(String... ids) {
      for (String id : ids) {
        TextField<String> t = new RequiredTextField<String>(id);
        add(t);
        add(new FormComponentLabel(id + "Lbl", t));
      }
    }
  }
}
